package org.example.service.bank.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.controller.rep.BankRatePageRep;
import org.example.dao.bank.BankDao;
import org.example.dao.bank.BankRateDao;
import org.example.entity.bank.BankEntity;
import org.example.entity.bank.BankRateEntity;
import org.example.service.bank.BankRateService;
import org.example.service.utils.Constant;
import org.example.service.utils.PageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class BankRateServiceImpl extends ServiceImpl<BankRateDao, BankRateEntity> implements BankRateService {

    @Resource
    private BankDao bankDao;


    @Override
    public PageUtils<BankRatePageRep> page(String apiRes) {
        return null;

    }

    @Override
    public List<BankRatePageRep> hotLists() {
        List<BankEntity> isHot = bankDao.selectList(new QueryWrapper<BankEntity>().eq("is_hot", Constant.ONE));
        List<BankRatePageRep> list = new ArrayList<>();
        isHot.forEach(obj -> {
            List<BankRateEntity> bankId = baseMapper.selectList(new QueryWrapper<BankRateEntity>()
                    .eq("bank_id", obj.getId()).orderByAsc("sequence"));
            List<BankRatePageRep.BankRateRep> rateList = new ArrayList<>();
            bankId.forEach(o -> {
                BankRatePageRep.BankRateRep rateBuild = BankRatePageRep.BankRateRep.builder()
                        .saveRate(o.getSaveRate())
                        .saveDate(o.getSaveDate())
                        .sequence(o.getSequence())
                        .saveCondition(o.getSaveCondition())
                        .saveRemark(o.getSaveRemark())
                        .build();
                rateList.add(rateBuild);
            });
            BankRatePageRep build = BankRatePageRep.builder().bankIcon(obj.getBankIcon()).name(obj.getName()).bankRateReps(rateList).build();
            list.add(build);
        });
        return list;
    }

    @Override
    public List<BankRatePageRep> lists() {
        List<BankEntity> isHot = bankDao.selectList(null);
        List<BankRatePageRep> list = new ArrayList<>();
        isHot.forEach(obj -> {
            List<BankRateEntity> bankId = baseMapper.selectList(new QueryWrapper<BankRateEntity>()
                    .eq("bank_id", obj.getId()).orderByAsc("sequence"));
            List<BankRatePageRep.BankRateRep> rateList = new ArrayList<>();
            bankId.forEach(o -> {
                BankRatePageRep.BankRateRep rateBuild = BankRatePageRep.BankRateRep.builder()
                        .saveRate(o.getSaveRate())
                        .saveDate(o.getSaveDate())
                        .sequence(o.getSequence())
                        .saveCondition(o.getSaveCondition())
                        .saveRemark(o.getSaveRemark())
                        .build();
                rateList.add(rateBuild);
            });
            BankRatePageRep build = BankRatePageRep.builder().bankIcon(obj.getBankIcon()).name(obj.getName()).bankRateReps(rateList).build();
            list.add(build);
        });
        return list;
    }
}
